package com.javalong.affairs.ui;

import com.javalong.affairs.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ImageTip extends LinearLayout {

	private ImageView iv_image;
	private TextView tv_tip;

	public ImageTip(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	@SuppressWarnings("deprecation")
	public ImageTip(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.ImageTip);
		int N = ta.getIndexCount();
		for (int i = 0; i < N; i++) {
			int attr = ta.getIndex(i);
			int resId = 0;
			switch (attr) {
			case R.styleable.ImageTip_imageHeight:
				resId = ta.getResourceId(R.styleable.ImageTip_imageHeight, 0);
				if (resId > 0) {
					iv_image.getLayoutParams().height = (int) ta.getResources()
							.getDimension(resId);
				} else {
					iv_image.getLayoutParams().height = (int) ta.getDimension(
							R.styleable.ImageTip_imageHeight, 0);
				}
				break;
			case R.styleable.ImageTip_imageWidth:
				resId = ta.getResourceId(R.styleable.ImageTip_imageWidth, 0);
				if (resId > 0) {
					iv_image.getLayoutParams().width = (int) ta.getResources()
							.getDimension(resId);
				} else {
					iv_image.getLayoutParams().width = (int) ta.getDimension(
							R.styleable.ImageTip_imageWidth, 0);
				}
				break;
			case R.styleable.ImageTip_textColor:
				resId = ta.getResourceId(R.styleable.ImageTip_textColor, 0);
				if (resId > 0) {
					tv_tip.setTextColor(ta.getResources().getColor(resId));
				} else {
					tv_tip.setTextColor(ta.getColor(
							R.styleable.ImageTip_textColor, 0));
				}
				break;
			case R.styleable.ImageTip_textSize:
				resId = ta.getResourceId(R.styleable.ImageTip_textSize, 0);
				if (resId > 0) {
					tv_tip.setTextSize(ta.getResources().getDimension(resId));
				} else {
					tv_tip.setTextSize(ta.getDimension(
							R.styleable.ImageTip_textSize, 0));
				}
				break;
			case R.styleable.ImageTip_imageSrc:
				resId = ta.getResourceId(R.styleable.ImageTip_imageSrc, 0);
				if (resId > 0) {
					iv_image.setImageDrawable(ta.getResources().getDrawable(
							resId));
				}
				break;
			case R.styleable.ImageTip_text:
				resId = ta.getResourceId(R.styleable.ImageTip_text, 0);
				if (resId > 0) {
					tv_tip.setText(ta.getResources().getString(resId));
				} else {
					tv_tip.setText(ta.getString(R.styleable.ImageTip_text));
				}
				break;
			}
		}
	}

	public ImageTip(Context context) {
		super(context);
		init();
	}

	private void init() {
		LayoutInflater inflater = LayoutInflater.from(getContext());
		inflater.inflate(R.layout.ui_image_tip, this);
		iv_image = (ImageView) findViewById(R.id.iv_image);
		tv_tip = (TextView) findViewById(R.id.tv_tip);
	}

	public void setScaleType(ScaleType scaletype){
		if(iv_image!=null){
			iv_image.setScaleType(scaletype);
		}
	}
	public void setImage(Drawable drawable) {
		if (iv_image != null) {
			iv_image.setImageDrawable(drawable);
		}
	}

	@SuppressWarnings("deprecation")
	public void setImage(int resId) {
		if (iv_image != null) {
			iv_image.setImageDrawable(getContext().getResources().getDrawable(
					resId));
		}
	}

	public void setText(String text) {
		if (tv_tip != null) {
			tv_tip.setText(text);
		}
	}

	public void setImageLevel(int level) {
		if (iv_image != null) {
			iv_image.setImageLevel(level);
		}
	}

	public void setImageSize(int resId) {
		int size = (int) getResources().getDimension(resId);
		iv_image.getLayoutParams().width = size;
		iv_image.getLayoutParams().height = size;
	}
}
